package jdbc1;

import java.sql.*;
import java.util.Scanner;

public class TestAdmin {
    public void JdbcStudy() {
    }
    private   String url="jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC";
    private   String user="root";
    private  String passWord="123456";

    public void JdbcStudy(String url, String user, String passWord) {
        this.url = url;
        this.user = user;
        this.passWord = passWord;
    }

    public static void main(String[] args) throws Exception {

        //1.注册操作数据的驱动 反射
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.使用api连接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC","root","123456");
        System.out.println("conn="+conn);
        //3.定义查询语句
        String sql="select * from tbl_student";
        //4.获取查询对象
        Statement statement = conn.createStatement();
        System.out.println("statement"+statement);
        //5.使用查询对象执行sql
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet="+resultSet);
        //6.解析数据
        while (resultSet.next())
        {
            int id = resultSet.getInt("stu_id");
            System.out.println(id);
        }
        //7.释放资源
        statement.close();
        conn.close();
    }
    //实现数据库入库操作
    public void insertData() throws Exception{
        Connection conn = conn();

        System.out.println("conn="+conn);
        String sql="insert into tbl_student values(5)";
        Statement statement = conn.createStatement();
        int i=statement.executeUpdate(sql);
        System.out.println("i="+i);
    }
    /*封装 连接数据库方法 将
     * 1.注册操作数据的驱动 反射
     * 2.使用api连接数据库
     * 封装成方法
     *
     * ***/
    public Connection conn(){
        Connection conn = null;
        try {
            //1.注册操作数据的驱动 反射
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.使用api连接数据库
            conn = DriverManager.getConnection(url,user,passWord);
            System.out.println("conn = " + conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    /**
     * 实现 数据库的 增删改的sql语句的执行
     *
     * @param conn 数据库连接对象
     * @param sql  执行的sql语句
     * @return int 执行结果
     */
    public static int stateObj(Connection conn, String sql) {
        int i = 0;
        try {
            Statement statement = conn.createStatement();
            i = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
    //实现删除数据
    public void deletData(String sql) throws Exception{
        Connection conn = conn();
        String sql1="delete from tbl_student where id=1";
        int i= JdbcStudy.stateObj(conn,sql1);
        System.out.println("删除数据："+i);
    }
    public boolean isAdmin(String admin,String passWord) throws Exception{
        Connection conn = conn();
        Statement statement = conn.createStatement();
        String sql="select *from manager where admin="+admin+" and password="+passWord;
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet.next();
    }
}
class test{
    public static void main(String[] args) {
        System.out.println("-----------------欢迎登录-----------");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String admin=scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        JdbcStudy jdbcStudy = new JdbcStudy();
        try {
            jdbcStudy.isAdmin(admin,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
